﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using MapCenterSystem.DataInfo;
using MapCenterSytem.Process;
using MapCenterSystem.ServiceProvider.GoogleMap;
using MapCenterSystem.Utility;
using MapCenterSystem.DefineType;
using System.Drawing;
using System.Collections.Generic;

using System.Threading;
using System.Resources;
using System.Globalization;

namespace MobiMap.Admin
{
    public partial class ManageService : System.Web.UI.Page
    {
        ResourceManager rm;
        CultureInfo ci;

        protected void Page_Load(object sender, EventArgs e)
        {            
            //Tab active
            Label lbService = (Label)Master.FindControl("tabService");
            lbService.CssClass = "tab_active";

            //Language
            SetLanguage();
        }

        public void SetLanguage()
        {
            rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
            if (Request.Cookies["language"] != null)
            {
                string language = Request.Cookies["language"].Value;
                Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
                LoadString(Thread.CurrentThread.CurrentCulture);

                return;
            }

            //Defaut language is Vietnamese
            Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
            ci = Thread.CurrentThread.CurrentCulture;
            LoadString(ci);
        }

        public void LoadString(CultureInfo ci)
        {
            lbFunctionTitle.Text = rm.GetString("Admin_FunctionAdd_Title", ci);
            btnAdd.Text = rm.GetString("Admin_AddPlace_btnAdd", ci);
            //rm.GetString("Admin_AddService_Success", ci);
            //rm.GetString("Admin_AddService_Error", ci);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string fullname = placeUC.FullName;
            string address = placeUC.Address;
            PlaceTypeInfo type = new PlaceTypeInfo();
            type.ID = int.Parse(placeUC.Type);
            Coordinate location = new Coordinate(double.Parse(placeUC.Latitude),
                                                    double.Parse(placeUC.Longitude));

            string phone = placeUC.Phone;
            string website = placeUC.Website;
            string info = placeUC.Info;
            string picture = "";

            PlaceInfo place = new PlaceInfo(type, fullname, address, phone, website, location, info);
            int id = PlaceEx.AddPlace(place);
            if (id != -1 && placeUC.Picture.PostedFile.FileName != "")
            {
                picture = "icon_" + id + ".png";
                string filename = AppDomain.CurrentDomain.BaseDirectory + @"Icons\Place\" + picture;
                placeUC.Picture.PostedFile.SaveAs(filename);
                PlaceEx.UpdatePlacePicture(id, picture);
            }

            Response.Redirect("ServiceDetail.aspx?id=" + id);
        }
    }
}
